import React, {Component} from 'react';
import {View, Text, Image, AsyncStorage, Linking, Platform, Alert} from 'react-native';
import {NavigationActions} from 'react-navigation';
import Button from '../../components/Button';
import styles from "../Root/style";
import px from "../../utils/px";
import * as GlobalStyles from './../../../src/style/GlobalStyles';
import * as constant from "../../constant";
import * as util from './../../utils';

export default class RootView extends Component {
	constructor(props) {
		super(props);
		this.state = {
			updating: false
		}
	}

	async updateAfterNavigation() {
		let userInfo = await AsyncStorage.getItem('userInfo');
		if (userInfo) {
			this.props.navigation.reset([NavigationActions.navigate({routeName: constant.navigation.NAVIGATOR_HOME})], 0)
		} else {
			this.props.navigation.reset([NavigationActions.navigate({routeName: constant.navigation.NAVIGATOR_LOGIN})], 0)
		}
	}

	downLoadApp = () => {
		let {updateUrl} = this.props;
		this.setState({
			updating: true
		})
		if (Platform.OS === 'android') {
			if (util.checkUri(updateUrl)) {
				Linking.openURL(updateUrl)
			} else {
				Alert.alert('警告', '下载地址格式不正确', [{text: 'OK', onPress: () => this.updateAfterNavigation()}])
			}
		} else {
			Linking.openURL(updateUrl)
		}
	}

	ignoreCurrentVersion = async () => {
		let {logicVersion} = this.props;
		let ignoreVersions = await AsyncStorage.getItem('ignoreVersions');
		if (ignoreVersions) {
			ignoreVersions = JSON.parse(ignoreVersions);
			if(ignoreVersions.indexOf(logicVersion) === -1){
				ignoreVersions.push(logicVersion)
			}
		} else {
			ignoreVersions = []
			ignoreVersions.push(logicVersion);
		}
		AsyncStorage.setItem('ignoreVersions', JSON.stringify(ignoreVersions));
		this.updateAfterNavigation();
	}

	lateAsk = () => {
		AsyncStorage.setItem('lateAskTime',Date.now().toString());
		this.updateAfterNavigation();
	}

	renderUpdate() {
		let {isForce, messages, displayVersion} = this.props;
		let {updating} = this.state;
		return (
				<View style={{flex: 1, backgroundColor: 'rgba(0,0,0,0.6)', ...GlobalStyles.H_HV_CENTER}}>
					<View>
						<Image
								style={styles.updateHeadImgStyle}
								source={require('../../assets/images/layer-head.png')}/>
						{
							updating ?
									<View style={styles.updateTitleWrapStyle}>
										<Text style={styles.updateTitleContentStyle}>正在更新中...</Text>
									</View> :
									<View style={styles.updateTitleWrapStyle}>
										<Text style={styles.updateTitleContentStyle}>发现新版本</Text>
										<Text style={styles.updateTitleContentVersionStyle}>{displayVersion}</Text>
									</View>
						}
						{
							updating ?
									<View style={styles.updateContentWrapStyle}>
										<Text style={styles.updatingContentTextStyle}>稍等片刻，马上就好</Text>
									</View> :
									<View style={styles.updateContentWrapStyle}>
										<Text style={[styles.updateContentTitleStyle, {marginBottom: 4}]}>更新内容:</Text>
										{
											messages.map((item, index) => {
												return (
														<Text key={index} style={styles.updateContentTitleStyle}>
															{item}
														</Text>
												)
											})
										}
									</View>
						}
						{
							updating ?
									null :
									<View style={styles.updateBtnWrapStyle}>
										{
											isForce ? null : <Button
													onPress={this.lateAsk}
													style={styles.updateBtnCancelStyle}
													containerStyle={styles.updateBtnContainerStyle}>稍后再说</Button>
										}
										<Button onPress={this.downLoadApp}
										        style={[styles.updateBtnOKStyle]}
										        containerStyle={[styles.updateBtnContainerStyle, {
											        backgroundColor: '#E83922',
											        width: px(isForce ? 520 : 240)
										        }]}>立即更新</Button>
									</View>
						}
						{
							updating ?
									null :
									<View style={{height: px(isForce ? 0 : 68), backgroundColor: '#FFFFFF'}}>
										{
											isForce ? null : <Button
													onPress={this.ignoreCurrentVersion}
													style={{color: '#333333', fontSize: px(24), marginTop: px(10)}}>忽略此版本</Button>
										}
									</View>

						}
					</View>
				</View>
		)
	}

	render() {
		let {needUpdate} = this.props;
		if (!needUpdate) {
			return null
		}
		return this.renderUpdate()
	}
}
